package com.reflect;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ControlAction extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp){
		
		
		try {
			String url = request.getRequestURI();
			String path = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
			Class clazz = null;
			System.out.println(url+"-----------"+path);
			if("u1".equals(path)){
				clazz = TestUser.class;
			}else{
				clazz = TestAnimal.class;
			}
			Method me = clazz.getDeclaredMethod("eat", null);
			Object obj = clazz.newInstance();
			me.invoke(obj, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
}
